/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main.KBProcess;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import main.Ontology.ElementOntologyFactory;
import org.xml.sax.SAXException;

/**
 *
 * @author Isaac Osesina
 */
public class UTEES_ProcessKB implements Serializable, Cloneable {

    KnowledgeBase kb;
    File kbFile;
    public UTEES_MatchTable documentOntology;
    
    private ElementOntologyFactory elementOntologyFactory;

    public UTEES_ProcessKB(File kbFile, UTEES_MatchTable documentOntology, ElementOntologyFactory elementOntologyFactory) {
        this.kbFile = kbFile;
        this.documentOntology = documentOntology;
        this.elementOntologyFactory = elementOntologyFactory;
    }

    public KnowledgeBase processKB(Set<Integer> docsToSkip, Set<Integer> docsToAccept) {
        return this.doParseInput_DOM(docsToSkip, docsToAccept);
    }

    private KnowledgeBase doParseInput_DOM(Set<Integer> docsToSkip, Set<Integer> docsToAccept) {
        try {
            UTEES_Parser_DOM newParser = new UTEES_Parser_DOM(elementOntologyFactory);
            newParser.readKB(kbFile, documentOntology, docsToSkip, docsToAccept);
            kb = newParser.getParsedKB();
            return kb;
        } catch (IOException ex) {
            Logger.getLogger(UTEES_ProcessKB.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    private KnowledgeBase doParseInput_SAX(Set<Integer> docsToSkip, Set<Integer> docsToAccept) {
        try {
            UTEES_Parser_SAX newParser = new UTEES_Parser_SAX(elementOntologyFactory);
            try {
                newParser.readKB(kbFile, documentOntology, docsToSkip, docsToAccept);
                kb = newParser.getParsedKB();
                return kb;
            } catch (IOException ex) {
                Logger.getLogger(UTEES_ProcessKB.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(UTEES_ProcessKB.class.getName()).log(Level.SEVERE, null, ex);
        } catch ( SAXException ex) {
            Logger.getLogger(UTEES_ProcessKB.class.getName()).log(Level.SEVERE, null, ex);
        } 
        return null;
    }

    public KnowledgeBase getKB() {
        return kb;
    }
}
